package com.pick.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pick.admin.entity.SysPermission;
import com.pick.admin.service.SysPermissionService;
import com.pick.base.common.PageQuery;
import com.pick.base.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@Tag(name = "权限管理")
@RestController
@RequestMapping("/system/permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @Operation(summary = "分页查询权限列表")
    @SaCheckPermission("system:permission:list")
    @GetMapping("/page")
    public Result<IPage<SysPermission>> pagePermissions(PageQuery pageQuery) {
        IPage<SysPermission> page = permissionService.pagePermissions(pageQuery);
        return Result.success(page);
    }

    @Operation(summary = "查询所有权限")
    @SaCheckPermission("system:permission:list")
    @GetMapping("/list")
    public Result<List<SysPermission>> listPermissions() {
        List<SysPermission> permissions = permissionService.list();
        return Result.success(permissions);
    }

    @Operation(summary = "根据ID查询权限")
    @SaCheckPermission("system:permission:query")
    @GetMapping("/{id}")
    public Result<SysPermission> getPermission(@PathVariable Long id) {
        SysPermission permission = permissionService.getById(id);
        return Result.success(permission);
    }

    @Operation(summary = "创建权限")
    @SaCheckPermission("system:permission:add")
    @PostMapping
    public Result<String> createPermission(@RequestBody SysPermission permission) {
        boolean success = permissionService.createPermission(permission);
        return success ? Result.success("创建成功") : Result.error("创建失败");
    }

    @Operation(summary = "更新权限")
    @SaCheckPermission("system:permission:edit")
    @PutMapping
    public Result<String> updatePermission(@RequestBody SysPermission permission) {
        boolean success = permissionService.updatePermission(permission);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "删除权限")
    @SaCheckPermission("system:permission:remove")
    @DeleteMapping("/{id}")
    public Result<String> deletePermission(@PathVariable Long id) {
        boolean success = permissionService.deletePermission(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }
}